<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div class="login-container">
    <!-- 左侧 Logo 区域 -->
    <div class="left-section">
      <div class="logo-area">
        <img src="" alt="Logo" class="logo" />
        <h2 class="welcome-text">{{ $t('login.welcome') }}</h2>
        <p class="welcome-desc">{{ isLogin ? $t('login.loginDesc') : $t('login.registerDesc') }}</p>
      </div>
    </div>

    <!-- 右侧登录表单区域 -->
    <div class="right-section">
      <div class="form-container">
        <div class="form-header">
          <h1>{{ isLogin ? $t('login.login') : $t('login.register') }}</h1>
          <!-- 切换登录/注册的标签 -->
          <div class="toggle-tabs">
            <span :class="{ active: isLogin }" @click="handleToggle(true)">{{
              $t('login.login')
            }}</span>
            <span :class="{ active: !isLogin }" @click="handleToggle(false)">{{
              $t('login.register')
            }}</span>
            <div class="slider" :class="{ 'slide-right': !isLogin }"></div>
          </div>
        </div>

        <!-- 表单切换动画 -->
        <Transition name="fade" mode="out-in">
          <!-- 登录组件 -->
          <LoginForm v-if="isLogin" @login-success="handleLoginSuccess" key="login" />
          <!-- 注册组件 -->
          <RegisterForm v-else @register-success="handleRegisterSuccess" key="register" />
        </Transition>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import LoginForm from './components/LoginForm.vue'
import RegisterForm from './components/RegisterForm.vue'

const router = useRouter()
const isLogin = ref(true)

// 处理切换动画
const handleToggle = (value: boolean) => {
  isLogin.value = value
}

// 登录成功处理
const handleLoginSuccess = () => {
  router.push('/')
}

// 注册成功处理
const handleRegisterSuccess = () => {
  isLogin.value = true
}
</script>

<style scoped>
.login-container {
  display: flex;
  min-height: 100vh;
  background-color: #f0f2f5;
  overflow: hidden;
}

.left-section {
  flex: 1;
  background: linear-gradient(135deg, #1890ff 0%, #1d39c4 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  padding: 2rem;
  position: relative;
  overflow: hidden;
}

.left-section::before {
  content: '';
  position: absolute;
  width: 200%;
  height: 200%;
  background: rgba(255, 255, 255, 0.1);
  transform: rotate(45deg);
  top: -50%;
  left: -50%;
}

.logo-area {
  text-align: center;
  position: relative;
  z-index: 1;
}

.logo {
  width: 120px;
  margin-bottom: 1rem;
  filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.1));
}

.welcome-text {
  font-size: 2.5rem;
  margin-bottom: 1rem;
  font-weight: 600;
}

.welcome-desc {
  font-size: 1.1rem;
  opacity: 0.9;
  max-width: 400px;
  margin: 0 auto;
}

.right-section {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
}

.form-container {
  width: 420px;
  background: white;
  padding: 2.5rem;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.05);
}

.form-header {
  margin-bottom: 2rem;
}

.form-header h1 {
  text-align: center;
  color: #1f1f1f;
  font-size: 1.8rem;
  margin-bottom: 1.5rem;
}

.toggle-tabs {
  display: flex;
  position: relative;
  background: #f5f5f5;
  border-radius: 6px;
  padding: 3px;
  margin: 0 auto;
  width: 240px;
  height: 36px;
}

.toggle-tabs span {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  padding: 0;
  cursor: pointer;
  position: relative;
  z-index: 2;
  transition: color 0.3s;
  font-weight: 500;
  user-select: none;
}

.toggle-tabs span.active {
  color: #fff;
}

.slider {
  position: absolute;
  left: 3px;
  top: 3px;
  width: calc(50% - 3px);
  height: calc(100% - 6px);
  background: #1890ff;
  border-radius: 4px;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 2px 4px rgba(24, 144, 255, 0.2);
}

.slider.slide-right {
  transform: translateX(100%);
}

/* 表单切换动画 */
.fade-enter-active,
.fade-leave-active {
  transition:
    opacity 0.3s ease,
    transform 0.3s ease;
}

.fade-enter-from {
  opacity: 0;
  transform: translateX(20px);
}

.fade-leave-to {
  opacity: 0;
  transform: translateX(-20px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
    min-height: 100vh;
  }

  .left-section {
    flex: none;
    height: 35vh;
    padding: 1.5rem;
  }

  .welcome-text {
    font-size: 1.8rem;
    margin-bottom: 0.5rem;
  }

  .welcome-desc {
    font-size: 1rem;
    padding: 0 1rem;
  }

  .logo {
    width: 80px;
    margin-bottom: 0.5rem;
  }

  .right-section {
    flex: none;
    padding: 1.5rem 1rem;
    min-height: 65vh;
    background: #fff;
  }

  .form-container {
    width: 100%;
    max-width: 100%;
    margin: 0;
    padding: 1.5rem;
    box-shadow: none;
    background: transparent;
  }

  .form-header h1 {
    font-size: 1.5rem;
    margin-bottom: 1rem;
  }

  .toggle-tabs {
    width: 200px;
    height: 32px;
  }

  .toggle-tabs span {
    font-size: 13px;
  }

  /* 调整表单内部元素间距 */
  :deep(.el-form-item) {
    margin-bottom: 1rem;
  }

  :deep(.el-input) {
    height: 40px;
  }

  :deep(.el-button) {
    height: 40px;
  }
}

/* 超小屏幕适配 */
@media (max-width: 375px) {
  .left-section {
    height: 30vh;
    padding: 1rem;
  }

  .welcome-text {
    font-size: 1.5rem;
  }

  .welcome-desc {
    font-size: 0.9rem;
  }

  .form-container {
    padding: 1rem;
  }

  .toggle-tabs {
    width: 180px;
    height: 30px;
  }

  .form-header h1 {
    font-size: 1.3rem;
  }

  :deep(.el-form-item__label) {
    font-size: 0.9rem;
  }
}

/* 处理横屏模式 */
@media (max-height: 600px) and (orientation: landscape) {
  .login-container {
    flex-direction: row;
  }

  .left-section {
    width: 35%;
    height: 100vh;
  }

  .right-section {
    width: 65%;
    height: 100vh;
    overflow-y: auto;
  }

  .welcome-text {
    font-size: 1.5rem;
  }

  .welcome-desc {
    font-size: 0.9rem;
  }
}

/* 处理安全区域 */
@supports (padding: env(safe-area-inset-bottom)) {
  .login-container {
    padding-bottom: env(safe-area-inset-bottom);
  }
}
</style>
